package com.project.livetv.activity;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.project.livetv.R;
import com.project.livetv.fragment.FocusFragment;

import java.util.ArrayList;
import java.util.List;

public class MessageActivity extends AppCompatActivity {
    private ViewPager viewPager;

    private TabLayout tableLayout;
    //碎片集合
    private List<FocusFragment> fragments;
    //标签
    private List<String> tabs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        tableLayout = (TabLayout) findViewById(R.id.tab_layout);
        viewPager = (ViewPager) findViewById(R.id.vp_messageactivity_viewpager);

        fragments = new ArrayList<>();
        tabs = new ArrayList<>();


        //初始化标签
        tableLayout.addTab(tableLayout.newTab().setText("好友"));
        tableLayout.addTab(tableLayout.newTab().setText("关注"));

        //设置碎片
        FocusFragment focusFragment = new FocusFragment();
        FocusFragment focusFragment1 = new FocusFragment();
        fragments.add(focusFragment);
        fragments.add(focusFragment1);
        //将标签设置成可滑动（防止标签过多）
        tableLayout.setTabMode(tableLayout.MODE_SCROLLABLE);
        //刷新
        adapter.notifyDataSetChanged();
        //绑定适配器
        viewPager.setAdapter(adapter);
        //tablayout绑定viewpager
        tableLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
        //viewpager绑定tablayout
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tableLayout));


    }


    private FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            Log.d("tag",fragments.size()+"");
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    };

}


